/*
 * Copyright 2016 Stuart Kent
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.example.bugshaker.slice;

import com.example.bugshaker.ResourceTable;
import com.example.bugshaker.fraction.*;
import com.example.mybugshaker.BugShaker;
import com.example.mybugshaker.flow.dialog.IDialogType;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.render.render3d.BuildConfig;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.RemoteException;

public class BaseAbilitySlice extends AbilitySlice {
    private static final int THREE =3;
    private static final int FOUR =4;
    private Boolean isVisible = false;
    private DirectionalLayout directionalLayout;

    private static final String EMAILS = "someone@example.com";

    private final HiLogLabel lable = new HiLogLabel(HiLog.LOG_APP, 0x00201, "BaseAbilitySlice");

    private int  currentFraction=-1;
    // 共享bugShaker实例
    private  void  applicationfangfa(AbilitySlice abilitySlice)
    {

        try {
            BugShaker.get(abilitySlice)
                    .setEmailAddresses(EMAILS)
                    .setEmailSubjectLine("BugShaker OpenHarmony App Feedback")
                    .setLoggingEnabled(BuildConfig.DEBUG)
                    .setAlertDialogType(IDialogType.APP_COMPAT)
                    .assemble()
                    .start();
        } catch (RemoteException e) {
            HiLog.error(lable,"BaseAbilitySlice", e);
        }
    }
    /**
     * onStart
     * */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        applicationfangfa(BaseAbilitySlice.this);
        super.setUIContent(ResourceTable.Layout_ability_base);
        // 添加点击事件
        Button button = (Button) findComponentById(ResourceTable.Id_more);

        // container 切换fraction
        directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_list);
        Text textUnsecuredAbility = (Text) findComponentById(ResourceTable.Id_unsecured);
        Text textSecuredAbility = (Text) findComponentById(ResourceTable.Id_secured);
        Text textMapViewAbility = (Text) findComponentById(ResourceTable.Id_mapView);
        Text textMapFraction = (Text) findComponentById(ResourceTable.Id_mapFraction);
        Text textSurfaceView = (Text) findComponentById(ResourceTable.Id_surfactView);

        // 添加默认的fraction
        addHomeFraction();

        // 点击...
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setMenuVisible();
            }
        });

        textUnsecuredAbility.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                layoutShow(0);
                setMenuVisible();
            }
        });
        textSecuredAbility.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                layoutShow(1);
                setMenuVisible();
            }
        });
        textMapViewAbility.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                layoutShow(2);
                setMenuVisible();
            }
        });
        textMapFraction.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                layoutShow(THREE);
                setMenuVisible();
            }
        });
        textSurfaceView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                layoutShow(FOUR);
                setMenuVisible();
            }
        });
    }

    /**
     * onStop
     * */
    @Override
    protected void onStop() {
        super.onStop();
        BugShaker.clearSharedInstance();
    }

    public final void layoutShow(int position){
        if (currentFraction==position){
            terminateAbility();
            return;
        }
        switch (position) {
            case 0:
//               加载Unsecured Ability
                ((FractionAbility)getAbility()).getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_main_container, new UnSecuredFraction())
                        .submit();
                break;
            case 1:
//               加载Secured Ability
                ((FractionAbility)getAbility()).getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_main_container, new SecuredFraction())
                        .submit();
                break;
            case 2:
//               加载MapViewFraction
                ((FractionAbility)getAbility()).getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_main_container, new MapViewFraction())
                        .submit();
                break;
            case THREE:
//               加载MapFraction
                ((FractionAbility)getAbility()).getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_main_container, new MapFraction())
                        .submit();
                break;
            case FOUR:
//               加载SurfaceViewFraction
                ((FractionAbility)getAbility()).getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_main_container, new SurfaceViewFraction())
                        .submit();
                break;
        }
        currentFraction=position;
    }
    // 添加一个默认显示的fraction
    private void  addHomeFraction(){
        ((FractionAbility)getAbility()).getFractionManager()
                .startFractionScheduler()
                .add(ResourceTable.Id_main_container, new UnSecuredFraction())
                .submit();
    }

    public final void setMenuVisible(){
        if (isVisible){
            directionalLayout.setVisibility(Component.HIDE);
            isVisible = false;
        }else {
            directionalLayout.setVisibility(Component.VISIBLE);
            isVisible = true;
        }
    }

    @Override
    public final void onActive() {
        super.onActive();
    }

    @Override
    public final void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
